// pages/perfect/perfect.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    Sdate: '请选择上架日期',
    doc: [{
      trackerName: '请选择跟单人'
    }],
    docIndex: 0,
    isMB: false,
    brand: [],
    size: [],
    colour: [],
    fabricList: [], //面料
    auxiliaryFabricList: [], //辅料
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    this.getDoc()
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/' + options.id,
      data: {},
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        for (let i in that.data.doc) {
          if (that.data.doc[i].trackerName.indexOf(res.data.data.trackerName) != -1) {
            that.data.docIndex = i
          }
        }
        for (let i in res.data.data.madeFabricList) {
          if (res.data.data.madeFabricList[i].fabricType == '面料') {
            app.globalData.fabricList.push(res.data.data.madeFabricList[i])
          } else {
            app.globalData.auxiliaryFabricList.push(res.data.data.madeFabricList[i])
          }
        }
        that.setData({
          list: res.data.data,
          img: res.data.data.designImage.split(','),
          Ddate: res.data.data.madeTemplateTime.split(' ')[0],
          Sdate: res.data.data.onlineTime.split(' ')[0],
          brand: res.data.data.brand.split(','),
          size: res.data.data.size.split(','),
          colour: res.data.data.color.split(','),
          docIndex: that.data.docIndex,
          fabricList: app.globalData.fabricList,
          auxiliaryFabricList: app.globalData.auxiliaryFabricList,
        })
      }
    })
  },
  onShow() {
    this.setData({
      fabricList: app.globalData.fabricList,
      auxiliaryFabricList: app.globalData.auxiliaryFabricList,
      // total: (fabricListPrice + auxiliaryFabricListPrice).toFixed(2),
    })
    this.getSettingList()
  },
  bindSDateChange(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      Sdate: e.detail.value
    })
  },
  bindDocChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      docIndex: e.detail.value
    })
  },
  addFabric: function(e) {
    wx.navigateTo({
      url: '/pages/addFabric/addFabric?type=' + e.currentTarget.dataset.type + '&i=false'
    })
  },
  edit: function(e) {
    wx.navigateTo({
      url: '/pages/editpurchas/editpurchas?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type
    })
  },
  edilt: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/addFabric/addFabric?type=' + e.currentTarget.dataset.type + '&index=' + e.currentTarget.dataset.index + '&i=false'
    })
  },
  put() {
    var arr = [...this.data.fabricList, ...this.data.auxiliaryFabricList]
    var data = {
      trackerId: this.data.doc[this.data.docIndex].trackerId,
      brand: this.data.brand.join(','),
      color: this.data.colour.join(','),
      size: this.data.size.join(','),
      onLineTime: this.data.Sdate + ' 23:59:59',
      madeFabricList: arr
    }
    app.req('designDiagram/done/' + this.data.id, data, 'PUT', res => {
      console.log(res)
      if (res.data.code == 200) {
        wx.showToast({
          title: res.data.msg,
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.errorMsg,
        })
      }
    })
  },
  //获取跟单人
  getDoc() {
    var that = this
    app.req('designDiagram/getPurchasingManager', '', 'GET', res => {
      console.log(res)
      if (res.data.code == 200) {
        that.setData({
          doc: that.data.doc.concat(res.data.data)
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.errorMsg,
        })
      }
    })
  },
  deMB() {
    this.setData({
      isMB: false
    })
  },
  add(e) {
    console.log(e)
    this.setData({
      isMB: true,
      name: e.currentTarget.dataset.name
    })
  },
  z_Submit(e) {
    switch (e.detail.target.dataset.name) {
      case '品牌':
        this.data.brand.push(e.detail.value.add)
        break
      case '尺码':
        this.data.size.push(e.detail.value.add)
        break
      case '颜色':
        this.data.colour.push(e.detail.value.add)
        break
    }
    this.setData({
      brand: this.data.brand,
      size: this.data.size,
      colour: this.data.colour,
      isMB: false
    })
  },
  z_dele(e) {
    console.log(e)
    switch (e.currentTarget.dataset.name) {
      case '品牌':
        this.data.brand.splice(e.currentTarget.dataset.index, 1)
        break
      case '尺码':
        this.data.size.splice(e.currentTarget.dataset.index, 1)
        break
      case '颜色':
        this.data.colour.splice(e.currentTarget.dataset.index, 1)
        break
    }
    this.setData({
      brand: this.data.brand,
      size: this.data.size,
      colour: this.data.colour
    })
  },
  onUnload: function() {
    app.globalData.fabricList = []
    app.globalData.auxiliaryFabricList = []
  },
  getSettingList() {
    var that = this
    wx.request({
      url: app.globalData.api + '/system/getSystemSetting',
      data: '',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success(res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            brandList: JSON.parse(res.data.data.brandList)
          })
        }
      }
    })
  },
  bindPickerChange: function (e) {
    this.data.brand.push(this.data.brandList[e.detail.value])
    this.setData({
      // index: e.detail.value,
      brand: this.data.brand
    })
  },
})